<!DOCTYPE html>

% # variable:
% # book, current_uuid, record_name, sidebar

% # 獲取 content 與 time_stamp
% if record_name is None or record_name == '':
    % content = book.content(current_uuid)

    % ftime_stamp = ''
    % mtime = book.mtime(current_uuid)
    % mtime_stamp = mtime.strftime(book.time_format)
% else:
    % content = book.record(current_uuid, name = record_name)

    % ftime = book.ftime(current_uuid, record_name)
    % ftime_stamp = ftime.strftime(book.time_format)
    % mtime = book.mtime(current_uuid)
    % mtime_stamp = mtime.strftime(book.time_format)
% end

% # 獲取 bookuuid
% bookuuid = book.book_uuid()

<html lang="">
    <head>
        <meta charset="utf-8" />

        <title>{{book.title(current_uuid)}} - {{book.title()}}</title>
        <meta name="description" content="{{book.title(current_uuid)}}" />

        <script src="/static/js/jquery-1.9.1.js"></script>
        <script src="/static/js/jquery-ui-1.10.1.custom.min.js"></script>

        <script src="/static/js/URI.js"></script>
        <script src="/static/js/shortcut.js"></script>
        <script src="/static/js/strftime.js"></script>
        <script src="/static/js/rangy-core.js"></script>
        <script src="/static/js/rangy-serializer.js"></script>
        <script src="/static/js/lonote.js"></script>

        <link rel="stylesheet" href="/static/css/jquery-ui/pepper-grinder/jquery-ui.css" />
        <link rel="stylesheet" href="/static/css/default.css" type="text/css" />
        <link rel="stylesheet" href="/static/css/article.css" type="text/css" />

        <!-- 使用者設定 -->
        {{!book.extra('extra_head') or ''}}
        <style>
            {{!book.extra('book.css') or ''}}
        </style>

    </head>

    <body>

        <!-- 時間記錄區 -->
        % if ftime_stamp: # 表示是 record 版本
            <time id="modified_time" datetime="{{mtime_stamp}}"></time>
            <time id="frozen_time" datetime="{{ftime_stamp}}" pubdate></time>
        % elif mtime_stamp: # 沒有 ftime_stamp 表示是一般版本
            <time id="modified_time" datetime="{{mtime_stamp}}" pubdate></time>
            <time id="frozen_time" datetime="{{ftime_stamp}}"></time>
        % end

        <!-- header -->
        % try:
            % parent_uuid, index = book.position(current_uuid)
            % ptitle = book.title(parent_uuid)
        % except: ptitle = ''
        <header>
            <div style="display: inline-block;">
                <h1 class="note_title">{{book.title(current_uuid)}}
                    <span style="display: inline-block; width: 100px;"></span>
                </h1>
                <br/>
                % if ptitle:
                    <a style="" href="/site/{{bookuuid}}/{{parent_uuid}}/"><p class="parent_title"> {{ptitle}} <img class="icon" src="/static/icon/other/back.svg" /></p></a>
                % end
            </div>
        </header>

        <!-- 筆記資料區 -->
        <article id='main_content' contenteditable="true">{{!content}}</article>

        <!-- footer -->
        <footer> - Designed by LoNote Project - </footer>

        <script>
            // 掛載執行與頁面相關的功能
            $(document).ready(function(){
                // 與 content 有關的功能
                lo.content.startup();

                // 復原選擇區位置
                rangy.init();
                function restoreSelection(){
                    var sel_str = localStorage['selection_of_' + location.pathname];
                    var root_elem = document.querySelector("#main_content");
                    if (sel_str != undefined && sel_str != null && sel_str != '' && rangy.canDeserializeSelection(sel_str, root_elem)) {
                        $('#main_content').focus();
                        try { rangy.deserializeSelection(sel_str, root_elem); }
                        catch (err){}
                    }
                }

                // 復原捲軸位置
                restoreSelection();

                lo.scr.restore();
                // 啟動各種快速鍵
                lo.key.img_ready();
                lo.key.search_ready();
                lo.key.noteop_ready();
                lo.key.content_ready();
                lo.key.edit_ready();
            });
        </script>

        <!-- 快速相對翻頁 -->
        <script>

            shortcut.add("Alt+PageUp",function(){
                % try:
                    % before_uuid = book.uuid_before(current_uuid)
                    location.assign("../{{!before_uuid}}/");
                % except:
                    alert('This is the top note');
                % end
            });

            shortcut.add("Alt+PageDown",function(){
                % try:
                    % next_uuid = book.uuid_next(current_uuid)
                    location.assign("../{{!next_uuid}}/");
                % except:
                    alert('This is the bottom note');
                % end
            });

            shortcut.add("Ctrl+Alt+PageUp",function(){
                % try:
                    % first_uuid = book.uuid_first(current_uuid)
                    location.assign("../{{!first_uuid}}/");
                % except:
                    % pass
                % end
            });

            shortcut.add("Ctrl+Alt+PageDown",function(){
                % try:
                    % last_uuid = book.uuid_last(current_uuid)
                    location.assign("../{{!last_uuid}}/");
                % except:
                    % pass
                % end
            });

        </script>

        <!-- 以非同步方式匯入 sidebar -->
        <!--
        <script>
            function sidebar_load(){
                function get_success(data){
                    $('body').append(data);
                };
                var sidebar_loc = location.href.replace('site','sidebar');
                $.get(sidebar_loc, get_success);
            };
            //$(window).one('load', sidebar_load);
            $(document).ready(sidebar_load);
        </script>
        -->

        {{!sidebar}}

        <!-- 讓瀏覽器將 last page 記錄為最後一次讀取或 focus 的頁面-->
        <script>
            localStorage['last'] = location.pathname;

            $(window).focus(function(e) {
                localStorage['last'] = location.pathname;
            });
        </script>

        <!-- 每次失去焦點時將 scroll 記錄下來 -->
        <script>
            $(window).blur(lo.scr.save);
            $(window).on('beforeunload', lo.scr.save);

            function saveSelection(){
                rangy.init();
                var sel = rangy.getSelection();
                var root_elem = document.querySelector("#main_content");
                try {
                    var sel_str = rangy.serializeSelection(
                        sel, true, root_elem);
                    localStorage['selection_of_' + location.pathname] = sel_str;
                        // 以上 omitChecksum 設 true 是因為有些會在顯示時改變
                        // DOM 結構的 library 會造成干擾，造成 checksum 不正確。
                        // 如 mathjax。
                } catch (err) {
                    localStorage.removeItem(
                        'selection_of_' + location.pathname);
                }
            }

            $(window).blur(saveSelection);
            $(window).on('beforeunload', saveSelection);
        </script>

    </body>

</html>
